package com.rchat.pocmini.activity.ble;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;

import com.rchat.pocmini.R;
import com.rchat.pocmini.bean2eventbus.BleBean;
import com.rchat.pocmini.bean2eventbus.ConnectBleBean;
import com.rchat.pocmini.bean2eventbus.ConnectedBleBean;
import com.rchat.pocmini.bean2eventbus.ScanBleBean;
import com.rchat.pocmini.bean2eventbus.ScanBleStateBean;
import com.rchat.pocmini.service.PocService;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ljd on 2017/8/29.
 */

public class BlePresenterImpl extends BleContract.Presenter {

    private List<BleBean> scanResultList;

    @Override
    public void onPStart() {
        super.onPStart();
        EventBus.getDefault().register(this);
        scanResultList = new ArrayList<>();
    }

    @Override
    void scan() {
        ScanBleBean b = new ScanBleBean();
        b.setTarget(this.getClass(), PocService.class, "scanBle");
        b.setCurrentEvent(ScanBleBean.SCAN_BLE);
        EventBus.getDefault().post(b);
    }

    @Override
    void onDisConnectedBle(final BleBean bean) {
        new AlertDialog.Builder(mActivity).setTitle("确认断开吗？")
                .setIcon(android.R.drawable.ic_dialog_info)
                .setPositiveButton(mActivity.getString(R.string.confirm), new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ConnectBleBean b = new ConnectBleBean();
                        b.setDeviceAddress(bean.getResult().getAddress());
                        b.setTarget(this.getClass(), PocService.class, "onDisConnectBle");
                        b.setCurrentEvent(ConnectBleBean.DISCONNECT_BLE);
                        EventBus.getDefault().post(b);
                        bean.setConnected(false);
                        mView.onUpdateBleList();
                    }
                })
                .setNegativeButton(mActivity.getString(R.string.cancle), new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 点击“返回”后的操作,这里不设置没有任何操作
                    }
                }).show();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusScanStatus(ScanBleStateBean b) {
        mView.scanStatus(b.getCurrentEvent());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusBleDataAdd(BleBean device) {
        for (BleBean s : scanResultList) {
            if (s.getResult().getAddress().equals(device.getResult().getAddress())) {
                return;
            }
        }
        scanResultList.add(device);
        mView.bleDataAdd(scanResultList);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusBleConnected(ConnectedBleBean b) {
        for (BleBean s : scanResultList) {
            if (s.getResult().getAddress().equals(b.getDeviceAddress())) {
                s.setConnected(true);
                mView.onBleConnected(scanResultList);
            }
        }

    }

    @Override
    public void onPDestroy() {
        EventBus.getDefault().unregister(this);
        super.onPDestroy();
    }
}
